/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.my_figure;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JSJSYS
 */
public class Userdao {
    static final String DRIVER_CLASS = "org.h2.Driver";
    //数据库连接URL，当前连接的是E:/H2目录下的mydb数据库
   // static final String JDBC_URL = "jdbc:h2:E:/H23/mydb";
     static final String JDBC_URL = "jdbc:h2:tcp://localhost/~/test";
      //static final String JDBC_URL = "jdbc:h2:tcp://localhost/mem:mydb";
    //用户名
     static final String USER = "yanghao";
    //密码
     static final String PASSWORD = "9264426";
     private static int num =1;
     private Statement stmt;
     private Connection conn;
     private String dboper="";
     private ResultSet rs;
     public boolean initDB() throws ClassNotFoundException
     {
          Class.forName(DRIVER_CLASS);
        // 根据连接URL，用户名，密码获取数据库连接
        try {
            conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
            stmt = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Userdao.class.getName()).log(Level.SEVERE, null, ex);
            return FALSE;
        }
        return TRUE;
     }
     static int getId()
     {
         return num++;
     }
     public void createBiao() throws SQLException
     {
         stmt.execute("DROP TABLE IF EXISTS login ");
         stmt.execute("CREATE TABLE login(id VARCHAR(20) PRIMARY KEY,psw varchar(20),sex varchar(5),tel varchar(12))");
          dboper+="添加表Login表，数据名字为用户名和密码"+"\n";
     }
     public void createvalues(Users tmp) throws SQLException
     {
         String stmp = "";
         if (tmp.sex == "" && tmp.tel == "") {
             stmp += "INSERT INTO Login(id,psw) VALUES('" + tmp.id + "','" + tmp.psw + "')";
             dboper += "账号：" + tmp.id + " 密码：" + tmp.psw + "\n";
         } else {
             stmp += "INSERT INTO Login VALUES('" + tmp.id + "','" + tmp.psw + "','" + tmp.sex + "','" + tmp.tel + "')";
             dboper += "账号：" + tmp.id + " 密码：" + tmp.psw + "性别:" + tmp.sex + "电话:" + tmp.tel + "\n";
         }
            
        
        stmt.executeUpdate(stmp);
     }
     public ResultSet selectalluser() throws SQLException
     {
         String stmp="select * from login";
         rs=stmt.executeQuery(stmp);
         return rs;
     }
     public void comparevalues(String ary[],String tmpn) throws SQLException
     {
         ResultSet result;
         result=this.selectalluser();
         while (result.next()) {
             ary[0] = result.getString(1);
             if (ary[0].equals(tmpn)) {
                 ary[1] = result.getString(2);
                 break;
             }
             else if(!ary[0].equals(tmpn))
             {
                 ary[0]="";
                 ary[1]="";
             }
           
         }
         result.close();
     }
     public boolean closeDB() throws SQLException
     {
         if(rs!=null)
             rs.close();
         stmt.close();
         conn.close();
         return TRUE;
     }
     public String getdboper()
     {
         return dboper;
     }
}
